/* Includes */
#include "include/menu.h"
#include "include/worlds.h"

/* Defines */
#define MENU_STATE_MAIN 0
#define MENU_STATE_WORLD 1
#define MENU_ACTION_NEWGAME 0
#define MENU_ACTION_QUIT 1
#define MENU_ACTION_BACK 1
#define MENU_POS_X 400
#define MENU_POS_Y 230
#define MENU_SPACING 10

/* Structs */
typedef struct world_list {
	char *name;
	char *filename;
	struct world_list *next;
} world;

/* globals */
int done = 0;
BITMAP *mbg = NULL;	/* Menu background */
world * root = NULL;	/* Root world */

void free_world_list()
{
	world * ptr = root;
	while(ptr)
	{
		/* Keep track of an item, move ptr to the next one */
		world *dummy = ptr;
		ptr = ptr->next;
		
		/* Free the item we kept track of */
		free(dummy->name);
		free(dummy->filename);
		free(dummy);
	} 
}

void main_menu()
{
	load_menu();
	
	int msta = 0; 		/* State variable */
	int msel = 0; 		/* What option is currently selected */
	int mopt = 1;		/* Number of options in the menu */
	
	/* draw the menu (else it won't show till we press a key) */
	blit(mbg, canvas, 0, 0, 0, 0, mbg->w, mbg->h);	/* Draw the background */
	draw_menu(msel, msta, &mopt); 		/* Draw the menu options/buttons */		
			
	while(!done) {
		
		if(menu_input(&msta, &msel, &mopt) == 1)
		{
	
			acquire_screen();
			blit(mbg, canvas, 0, 0, 0, 0, mbg->w, mbg->h);	/* Draw the background */
			draw_menu(msel, msta, &mopt); 		/* Draw the menu options/buttons */
			release_screen();

			sync();
		} 
		else rest(1);	/* don't hog the CPU */
	} return;
}

void draw_menu(int msel, int msta, int *mopt)
{
	switch(msta)
	{
		case MENU_STATE_MAIN:
			textprintf_ex(canvas, font, MENU_POS_X, MENU_POS_Y, 
					makecol(255, (msel == 0 ? 0 : 255), 0), 
					-1, "->Load World");

			textprintf_ex(canvas, font, MENU_POS_X, MENU_POS_Y + MENU_SPACING, 
					makecol(255, (msel == 1 ? 0 : 255), 0), 
					-1, "<-Quit");
			break;
		case MENU_STATE_WORLD:
		{
			world * ptr = root;
			int i = 0;

			while(ptr) {
				textprintf_ex(canvas, font, MENU_POS_X, MENU_POS_Y + i * 10,
						makecol(255, (msel == i ? 0 : 255), 0),
						-1, "%s / %s", ptr->name, ptr->filename);
				ptr = ptr->next;				
				++i;
				*mopt = i;
			}

			textprintf_ex(canvas, font, MENU_POS_X, MENU_POS_Y + (*mopt) * 10, 
					makecol(255, (msel == *mopt ? 0 : 255), 0), 
					-1, "<-Back");
		}
		break;
	}
}

int menu_input(int *msta, int *msel, int *mopt)
{
	if(keypressed())
	{
		int val = readkey() >> 8;
		switch(val)
		{
		case KEY_UP:
			if(--*msel < 0) *msel = *mopt;
			break;
		case KEY_DOWN:
			if(++*msel > *mopt) *msel = 0;	
			break;
		case KEY_ENTER:
			switch(*msta)	/* Select the state */
			{
			case MENU_STATE_MAIN:
				switch(*msel)
				{
				case MENU_ACTION_NEWGAME:
					populate_world_list("worlds/*.zip");	
					*msta = MENU_STATE_WORLD;
					break;
				case MENU_ACTION_QUIT:
					free_menu();
					done = 1;
					return 0;
					break;
				}
				break;
			case MENU_STATE_WORLD:
				if(*msel < *mopt) { 	/* Loading/Playing the world */
					world_demo(/* "worlds/Texan.aw" */ "worlds/ziptest.zip");

				}
				if(*msel == *mopt) { 		/* Back button */		

					/* Set the variables so it'll go back to main menu */
					free_world_list(); 
					*msta = MENU_STATE_MAIN;
					*msel = 0;
					*mopt = 1;
				}
				break;
			}
			break;
		}

		clear_keybuf();
		return 1;	/* redraw on an action */
	}
	return 0; /* don't redraw */
}

void populate_world_list(char *path) 
{ 
	struct al_ffblk	FileInfo;
	world **last = &root;	/* should always be empty */

	if(al_findfirst(path, &FileInfo, FA_LABEL) == 0) {
		char * world_name = NULL;		
		do {
			if((world_name = verify_world(FileInfo.name))) {
				*last = (world*)malloc(sizeof(world));

				(*last)->name = world_name;

				(*last)->filename = malloc(strlen(FileInfo.name));
				strcpy((*last)->filename, FileInfo.name);

				(*last)->next = NULL;

				last = &((*last)->next);
			}
		} while (al_findnext( &FileInfo) == 0);
	}
	al_findclose(&FileInfo); 
/*
	if( al_findfirst( path, &FileInfo, FA_LABEL) == 0) {		
		char * world_name = NULL; 
		do { 
			amazolog("Checking if '%s' is a valid world.\n", FileInfo.name);
			if((world_name = verify_world(FileInfo.name))) {
					
				*last = (world*)malloc(sizeof(world));
								
				(*last)->name = world_name;
				
				(*last)->filename = malloc(strlen(FileInfo.name)+1);
				strcpy((*last)->filename, FileInfo.name);

				(*last)->next = NULL;
				
				amazolog("World Name[%d] %s\n", i, (*last)->name);
				amazolog("World File[%d] %s\n", i, (*last)->filename);

				last = &((*last)->next);
			}

			i++;
		} while (al_findnext( &FileInfo) == 0); 
	}
	al_findclose( &FileInfo);
	amazolog("World list populated.\n");
*/
}

char * verify_world(const char * filename)
{
	char path[128];
	strcpy(path, "worlds/");
	strcat(path, filename);
	
	ZZIP_DIR *dir = zzip_dir_open(path, 0);

	ZZIP_FILE *file = zzip_file_open(dir, "world.ini", 0);

	

 	if (file) {
 		#define BUF_SIZE	4096
 		char buf[BUF_SIZE];
   		while(zzip_file_read(file, buf, BUF_SIZE))
	   		am_parse_ini_val(buf, "WORLD ID", "NAME"); 
   		zzip_file_close(file);
 	}  else {
		return 0;
	}
	
	char * tmp = malloc(strlen("Texas Donkeypunch")+1);
	strcpy(tmp, "Texas Donkeypunch");
	return tmp;
}

void load_menu()
{
	if((mbg = load_bitmap("mbg.png", NULL)) == NULL) {
		amazolog("Failed to load menu background\n");
		mbg = create_bitmap(1,1);
		return;
	}
}

void free_menu()
{
	destroy_bitmap(mbg); /* Release the background */
}
